package com.boruiimage.demo.controller;

import com.boruiimage.demo.doMain.Result;
import com.boruiimage.demo.doMain.elisor;
import com.boruiimage.demo.repository.registerDao;
import com.boruiimage.demo.repository.registerMemberUserDao;
import com.boruiimage.demo.utils.AES;
import com.boruiimage.demo.utils.ALiMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.Random;

@Controller
public class registeController {
    @Autowired
    private registerDao rd;

    @RequestMapping("/addUser")
    @ResponseBody
    //注册
    public Result addUser(@RequestParam(value = "userName")String userName,
                          @RequestParam(value = "userPassword")String userPassword,
                          @RequestParam(value = "code")String code,
                          HttpServletRequest request,HttpServletResponse response){
        int adduser=rd.addUser(userName,userPassword,null,code,request);
        Result rs=new Result();
        if(adduser==0){
            request.getSession().setMaxInactiveInterval(86400);//设置一天的session时间
            request.getSession().setAttribute("userName",userName);
            rs.setMsg("注册成功");
        }else if(adduser==1){
          rs.setMsg("注册失败！");
        }else if(adduser==2){
            rs.setMsg("该手机号已被注册！");
        }else if(adduser==3){
            rs.setMsg("验证码有误！");
        }else if(adduser==4){
            rs.setMsg("Ok!");
        }
          return  rs;
    }
    @RequestMapping("/upUser")
    @ResponseBody
    //修改密码
    public Result upUser(@RequestParam(value = "userName")String userName,
                         @RequestParam(value = "userPassword")String userPassword,
                         @RequestParam(value = "code")String code,HttpServletRequest request){
          return rd.upUser(userName,userPassword,code,request);
    }

    //APP端登录
      @Autowired
private registerMemberUserDao rmud;
    @RequestMapping("/login")
    @ResponseBody
    public Result shows(@RequestBody Map<String, String> map) {
        Result rs = rd.selectUser(map.get("userName"), map.get("password"));
        String result=rmud.meSelectByUsername(map.get("userName"));//查询会员类型
        rs.setResult(result);
        System.out.println(map.get("userName") + "用户名");
        System.out.println(map.get("password") + "密码");
        return rs;
    }
    //APP端注册
    //短信接口
    @RequestMapping( "/mas")
    @ResponseBody
    public Result sendSMS(@RequestBody Map<String, String> map) {
        Result rs = new Result();
        Result rc = new Result();
        String randum = null;
        String passwordCode = null;
        String phoneCode = AES.encrypt(map.get("phone"));
        if (map.get("password") != null) {
            passwordCode = AES.encrypt(map.get("password"));
        }
        if (map.get("type").equals("0")) {
            System.out.println(phoneCode + "phoneCode");
            Result rse = rd.SeletctByuserNameS(map.get("phone"));//验证用户名是否存在
            System.out.println(rse);
            if (rse.getMsg().equals("成功")) {//如果用户名不存在返回成功发送验证码
                randum = ALiMessage.show(map.get("phone"));
                rd.addmess(map.get("phone"), randum,null);
                return rse;
            } else {
                return rse;
            }
        }
        if (map.get("type").equals("1")) {  //注册
            String rondum = rd.Select(map.get("phone"));
            if (rondum.equals(map.get("code"))) {//如果后台获取的验证码和前端传过来的匹配成功
                rs.setMsg("成功");
                rs.setResult("0");
                rd.addUserS(phoneCode, passwordCode,null);
                //rd.de();
            } else {
                rs.setMsg("失败");
            }
        }
        if (map.get("type").equals("2")) {//登录
            String rondum = rd.Select(map.get("phone"));
            System.out.println(ALiMessage.show(map.get("phone"))+"ssss");
            String result=rmud.meSelectByUsername(phoneCode);//查询会员类型
            if (rondum.equals(map.get("code"))) {//如果后台获取的验证码和前端传过来的匹配成功
                rs.setMsg("成功");
                rs.setResult(result);
               // rd.de();
            } else {
                rs.setMsg("失败");
            }
        }
        if (map.get("type").equals("3")) {
            System.out.println(phoneCode + "phoneCode");
            Result rse = rd.SeletctByuserNameS(map.get("phone"));//验证用户名是否存在
            if (rse.getMsg().equals("失败")) {//如果用户名存在返回成功发送验证码
                randum = ALiMessage.show(map.get("phone"));
                rd.addmess(map.get("phone"), randum,null);
                rc.setMsg("成功");
                return rc;
            } else {
                rc.setMsg("失败");
                return rc;
            }
        }
        if(map.get("type").equals("4")){//修改密码
            Result rse = rd.SeletctByuserNameS(map.get("phone"));//验证用户名是否存在
            if(rse!=null){
                randum = ALiMessage.show(map.get("phone"));
                rd.addmess(map.get("phone"), randum,null);
                rse.setMsg("成功");
                return  rse;
            }else {
                rse.setMsg("失败");
                return  rse;
            }
        }
        if(map.get("type").equals("5")){
            String rondum = rd.Select(map.get("phone"));
            if (rondum.equals(map.get("code"))) {
                if(rd.upUsers(AES.encrypt(map.get("phone")),AES.encrypt(map.get("password")))>0){
                    rs.setMsg("成功");
                    //rd.de();
                  return  rs;
                }else {
                    rs.setMsg("失败");
                    return  rs;
                }

            }
        }
        System.out.println(rs);
        return rs;
    }
    //刷新查询会员状态
    @RequestMapping("/vipCode")
    @ResponseBody
    public Result selectCode(@RequestBody Map<String,String>map){
        String result=rmud.meSelectByUsername(map.get("userName"));//查询会员类型
        Result rs=new Result();
        rs.setResult(result);
        return  rs;
    }
    //查询会员是否有次数使用次数加-
    @RequestMapping("/selecCode")
    @ResponseBody
    public Result selectBycode(@RequestBody Map<String,String>map){
        String imgDetil="http://52borui.cn:8081/" +map.get("imgName");
        Result rs=rmud.SelectBydata(map.get("userName"),Integer.parseInt(map.get("code")),map.get("imgName"),imgDetil,map.get("imgResult"),null);
        return  rs;
    }
    //添加司法信息
    @RequestMapping("/addEliser")
    @ResponseBody
    public Result  addEliser(@RequestBody Map<String,String>map){
        Random random =new Random();
        int imgid=random.nextInt(10000);
        Result rs=new Result();
        int i=rmud.addElisor(map.get("name"),map.get("phone"),map.get("userName"),map.get("email"),map.get("context"),null,imgid);
        if(i>0){
            rs.setMsg("成功");
        }else {
            rs.setMsg("失败");
        }
        return  rs;
    }
    //查询该用户的最后一张的图片地址，鉴定结果，时间,是否有鉴定记录
    @RequestMapping("/selectImgInfos")
    @ResponseBody
    public Result showImgInfo(@RequestBody Map<String,String>map ){
        return rmud.SelectByifo(map.get("userName"));
    }
    //查询司法信息
    @RequestMapping("/selectElisor")
    @ResponseBody
    public elisor showElisor(@RequestBody Map<String,String>map){
         return rmud.selectElisor(map.get("userName"),Integer.parseInt(map.get("imgId")));
    }
}
